package org.liyufeng.project.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;


@Controller
@RequestMapping("/user")
public class UserAction {
	
	@RequestMapping("/tt")
	public String test(HttpServletRequest request) throws Exception{
		request.setAttribute("request", "请求参数");
		
		return "redirect:/index.jsp";
	}

	@RequestMapping("/remove")
//	@RequestParam(value="id",defaultValue="0")表示如果客户端请求参数的值为空，默认将0赋值给形参userId
	public String del(@RequestParam(value="id",defaultValue="0")int userId) throws Exception{
		System.out.println("id：" + userId);
		return "/index.jsp";
	}
	
//	method=RequestMethod.POST表示该访问只支持post方式
	@RequestMapping(value="/findById/{id}",method=RequestMethod.GET)
//	@RequestMapping("/fingById/{id}")指明客户端的请求路径；{id}表示符号占位符
//	@PathVariable("id")将符号占位符id的内容取出来，赋值给形参userId
	public String findById(@PathVariable("id") int userID) throws Exception{
		System.out.println("用户ID：" + userID);
		return "/index.jsp";
	}
	
//	@RequestMapping("/reg")
//	@ModelAttribute("user")表示将bean封装在request作用域中，键为user
//	@Valid表示当前请求封装的实体类，需要验证
//	@RequestParam("head") CommonsMultipartFile myFile表示将文件表单head上传的内容复制给myFile
//	public String add(@ModelAttribute("user") @Valid UserBean bean,
//			BindingResult result,HttpServletRequest request,
//			@RequestParam("head") CommonsMultipartFile myFile) throws Exception{
////		判断验证是否有错误信息
//		if(result.hasErrors()){
//			return "/reg.jsp";
//		}
//		System.out.println("bean：" + bean);
//		
////		得到上传文件名
//		String fileName = myFile.getOriginalFilename();
//		fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
//		
////		得到上传文件的真实路径
//		String filePath = request.getSession().getServletContext().getRealPath("/img/" + fileName);
//
////		将数据写入文件
//		myFile.getFileItem().write(new File(filePath));
//		
//		return "/index.jsp";
//	}
	
	
}
